﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Tao.FreeGlut;
using Tao.OpenGl;
using Tao.Platform.Windows;

namespace N_dimenslArr
{
    class InfoScene
    {
        private double sceneWidth;
        private double sceneHeight;
        private int viewportWidth;
        private int viewportHeight;
        private double scaleRatio;
        private double aspectRatio;
        private int gratingPeriod;
        
        public InfoScene()
        { 
        }

        public InfoScene(
               double sceneWidth,
               double sceneHeight,
               int viewportWidth,
               int viewportHeight,
               int gratingPeriod)
        {
            this.Shift = new double[2] { 0, 0 };
            this.Zoom = 1;
            Debug.Assert(sceneHeight > 0, "sceneHeight > 0");
            Debug.Assert(sceneWidth > 0, "sceneWidth > 0");
            Debug.Assert(viewportHeight > 0, "viewportHeight > 0");
            Debug.Assert(viewportWidth > 0, "viewportWidth > 0");
            Debug.Assert(gratingPeriod != 0, "gratingPeriod != 0");
            this.sceneHeight = sceneHeight;
            this.sceneWidth = sceneWidth;
            this.viewportWidth = viewportWidth;
            this.viewportHeight = viewportHeight;
            this.gratingPeriod = gratingPeriod;
        }

        public bool isActiveScene { get; set; }

        public double[] Shift { get; set; }

        public double Zoom { get; set; }

        public double Rotate { get; set; }

        public void InitGraphicsSystem()
        {
            Gl.glClearColor(255, 255, 255, 1);
            // установка порта вывода в соотвествии с размерами элемента anT 
            Gl.glViewport(0, 0, this.viewportWidth, this.viewportHeight);
            // настройка проекции 
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            var maxDimension = Math.Max(this.sceneWidth, this.sceneHeight);
            Gl.glOrtho(0, maxDimension, maxDimension, 0, -0.5, 0.5);
            this.aspectRatio = Convert.ToDouble(this.viewportHeight) / Convert.ToDouble(this.viewportWidth);
            this.scaleRatio = Convert.ToDouble(this.viewportWidth) / maxDimension;
            Gl.glScaled(this.aspectRatio, 1, 1);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        public void Move(int deltaX, int deltaY)
        {
            this.Shift[0] += Convert.ToDouble(deltaX) / (this.aspectRatio * this.scaleRatio);
            this.Shift[1] += Convert.ToDouble(deltaY) / (this.aspectRatio * this.scaleRatio);
            this.Render();
        }

        public void SetZoom(double delta)
        {
            this.Zoom += delta / (this.aspectRatio * this.scaleRatio);
            this.Render();
        }

        public void ClrScr()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
        }

        private void SwapBuffers()
        {
            Gl.glFlush();
        }


        public void Render()
        {
            this.ClrScr();

            Gl.glLoadIdentity();
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glPushMatrix();

            Gl.glPushAttrib(Gl.GL_ALL_ATTRIB_BITS);

            Gl.glLineWidth(1);
            Gl.glColor4f(0, 0, 0, 1);
           
            ///
            Gl.glPushAttrib(Gl.GL_POINT);
            Gl.glPushAttrib(Gl.GL_COLOR_ALPHA_PAIRING_ATI);

            Gl.glPointSize(9);
            Gl.glColor4f(1.0f, 0, 0, 1.0f);

            Gl.glBegin(Gl.GL_POINTS);

            //Gl.glVertex3d(8.57, 3.2, 0);
            Gl.glVertex4d(3.65, 13.2, 0, 1);

            Gl.glEnd();

            Gl.glPopAttrib();
            Gl.glPopAttrib();
            ///
            
            Gl.glPopAttrib();
            Gl.glPopMatrix();

            this.SwapBuffers();
        }
    }
}
